<?php

class ResourceTypeDB {

    public static function getResourceTypeByID($resource_type_id) {//works
        try {
            $query = "SELECT * FROM resource_type WHERE resource_type_id = :resource_type_id";
            $dbh = DatabaseConnection::getDB();
            $stmt = $dbh->prepare($query);
            $stmt->bindParam(':resource_type_id', $resource_type_id);
            $stmt->execute();
            $rowsAffect = $stmt->rowCount();
            if ($rowsAffect > 0) {
                $result = $stmt->fetch(PDO::FETCH_OBJ);
                return self::getResourceTypeFromPDO($result);
            }
        } catch (PDOException $e) {
            echo "Error: " . $e->getMessage();
        }
    }

    private static function getResourceTypeFromPDO($result) {//works
        $resource_type = new ResourceType();
        $resource_type->setResource_type_id($result->resource_type_id);
        $resource_type->setResource_type_name($result->resource_type_name);
        return $resource_type;
    }

    public static function getAllResourceTypes() {
        try {
            $query = "SELECT * FROM resource_type";
            $dbh = DatabaseConnection::getDB();
            $stmt = $dbh->prepare($query);
            $stmt->execute();
            $rowsAffected = $stmt->rowCount();
            if ($rowsAffected > 0) {
                $result = $stmt->fetchAll(PDO::FETCH_OBJ);
                $inventory = new Inventory();
                foreach ($result as $item) {
                    $resource_type = self::getResourceTypeFromPDO($item);
                    $inventory->attach($resource_type, $resource_type->getResource_type_id());
                }
                return $inventory;
            } else {
                return null;
            }
        } catch (PDOException $e) {
            echo "Error: " . $e->getMessage();
        }
    }
    
    public static function getResourceTypeNameByID($resource_type_id) {//works
        try {
            $query = "SELECT resource_type_name FROM resource_type WHERE resource_type_id = :resource_type_id";
            $dbh = DatabaseConnection::getDB();
            $stmt = $dbh->prepare($query);
            $stmt->bindParam(':resource_type_id', $resource_type_id);
            $stmt->execute();
            $rowsAffect = $stmt->rowCount();
            if ($rowsAffect > 0) {
                $result = $stmt->fetchColumn();
                return $result;
            }
        } catch (PDOException $e) {
            echo "Error: " . $e->getMessage();
        }
    }

}

?>
